package com.zhj.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhj.product.converter.CategoryConverter;
import com.zhj.product.entity.dto.CategoryDTO;
import com.zhj.product.entity.dto.GoodItemsDTO;
import com.zhj.product.entity.dto.ItemsDTO;
import com.zhj.product.entity.pojo.Category;
import com.zhj.product.entity.pojo.Items;
import com.zhj.product.mapper.CategoryMapper;
import com.zhj.product.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;


    @Override
    public List<Category> selectCategoryRoot() {
        //所有type为1的数据
        LambdaQueryWrapper<Category> queryWrapper = new QueryWrapper<Category>().lambda().eq(Category::getType, 1);
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<CategoryDTO> selectCategoryChildren(Integer fatherId) {
        LambdaQueryWrapper<Category> queryWrapper = new QueryWrapper<Category>().lambda().eq(Category::getFatherId, fatherId);
        //根据id查询出所有的父级节点
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        List<CategoryDTO> categoriesDto = new ArrayList<>(categories.size());
        if (categories.size() <= 0) {
            return categoriesDto;
        }
        //循环递归查询后续子节点
        categories.forEach(category -> {
            CategoryDTO categoryDTO = CategoryConverter.INSTANCE.poToDto(category);
            //获取到id后根据父级节点的id递归查询分类
            categoryDTO.setSubCatList(this.selectCategoryChildren(category.getId()));
            categoriesDto.add(categoryDTO);
        });
        return categoriesDto;
    }
}
